/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import entities.Labor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

/**
 *
 * @author MinhDuc
 */
public class LaborDA {

    Connection connect = null;

    public LaborDA() {
        connect = ConnectDatabase.getConnection();
    }

    public Vector<Labor> getAll() {
        Vector<Labor> data = new Vector<>();
        String select = "select * from Labor";
        try {
            Statement stm = connect.createStatement();
            ResultSet rs = stm.executeQuery(select);
            while (rs.next()) {

                Labor labor = new Labor();
                labor.setLaborID(rs.getNString(1));
                labor.setName(rs.getNString(2));
                labor.setAddress(rs.getNString(3));
                labor.setPhoneNumber(rs.getInt(4));
                labor.setStatus(rs.getNString(5));
                labor.setWeight(rs.getNString(6));
                labor.setHeight(rs.getNString(7));
                labor.setGender(rs.getBoolean(8));
                labor.setFamilyDetail(rs.getNString(9));
                labor.setPersonRefer(rs.getNString(10));
                labor.setExperience(rs.getNString(11));
                labor.setSalaryGrade(rs.getNString(12));
                labor.setBuilding(rs.getNString(13));
                labor.setPermanent(rs.getBoolean(14));

                data.add(labor);
            }
            rs.close();
            stm.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return data;
    }

    public Labor getLaborByID(String LaborID) {
        Labor labor = null;
        String sql = "SELECT * FROM Labor WHERE [LaborID] = ?";
        try {
            PreparedStatement pstm = connect.prepareStatement(sql);
            pstm.setNString(1, LaborID);
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {
                labor = new Labor();

                labor.setLaborID(rs.getNString(1));
                labor.setName(rs.getNString(2));
                labor.setAddress(rs.getNString(3));
                labor.setPhoneNumber(rs.getInt(4));
                labor.setStatus(rs.getNString(5));
                labor.setWeight(rs.getNString(6));
                labor.setHeight(rs.getNString(7));
                labor.setGender(rs.getBoolean(8));
                labor.setFamilyDetail(rs.getNString(9));
                labor.setPersonRefer(rs.getNString(10));
                labor.setExperience(rs.getNString(11));
                labor.setSalaryGrade(rs.getNString(12));
                labor.setBuilding(rs.getNString(13));
                labor.setPermanent(rs.getBoolean(14));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return labor;
    }
}
